#include "ServerGame.h"
#include "Utils.h"

ServerGame::ServerGame(int numMoons)
{
	allMoons = new vector<ServerMoon>();
	//create numMoons moons to fill the vector
	for(int i = 0; i < numMoons; i++)
	{
		ServerMoon * moon = new ServerMoon(i, STARTING_POPULATION);
		(*allMoons).insert((*allMoons).end(), *moon);
	}
	allPlayers = new vector<ServerPlayer>();
	

}

void ServerGame::update()
{
	timePrev = timeNow;
	timeNow = clock();
	if ( !(timePass = timeNow-timePrev) ) { return; }
	int timeStep = timePass*timeDT;
	
	//basically do all updating necessary
	for(vector<ServerMoon>::iterator it = (*allMoons).begin(); it != (*allMoons).end(); ++it)
	{
		ServerMoon currentMoon = *it;
		if(currentMoon.getPlayerId() != -1)
		{
			currentMoon.update(timeStep);
		}
	}	
}

void ServerGame::addPlayer(int pPlayerId, string pPlayerName)
{
	vector<int>* moons = new vector<int>();
	//find an available moon to assign to the new player
	for(vector<ServerMoon>::iterator it = (*allMoons).begin(); it != (*allMoons).end(); ++it)
	{
		ServerMoon currentMoon = *it;
		if(currentMoon.getPlayerId() == -1)
		{
			//assigning player to the moon
			currentMoon.assign(pPlayerId);
			(*moons).insert((*moons).end(), currentMoon.getMoonId());
			//you only get 1!
			break;
		}
	}
	(*allPlayers).insert((*allPlayers).end(), 
					  *(new ServerPlayer(pPlayerId, pPlayerName, moons)));
}

void ServerGame::removePlayer(int pPlayerId)
{
	for(vector<ServerPlayer>::iterator it = (*allPlayers).begin(); it != (*allPlayers).end(); ++it)
	{
		ServerPlayer currentPlayer = *it;
		vector<int> moons = *currentPlayer.getMoons();
		for(vector<int>::iterator it2 = (moons).begin(); it2 != (moons).end(); ++it2)
		{
			int x = *it2;
			ServerMoon * moon = findMoon(x);
			//assign to -1
			(*moon).assign(-1);
		}
	}
}

ServerMoon *ServerGame::findMoon(int moonId)
{
	for(vector<ServerMoon>::iterator it = (*allMoons).begin(); it != (*allMoons).end(); ++it)
	{
		ServerMoon currentMoon = *it;
		if(currentMoon.getMoonId() == moonId)
		{
			return &(*it);
		}
	}
	return NULL;
}

string ServerGame::serialize()
{
	//serializing the game...
	string result = "";
	for(vector<ServerMoon>::iterator it = (*allMoons).begin(); it != (*allMoons).end(); ++it)
	{
		//moon string:  Moon,moonId,playerId,population,missileResearchLevel,shieldResearchLevel,reproduction,production,research,
		//(no newline)  currentProductionTaskName,BuildProgress,currentResearchTaskName,ResearchProgress
		//(no newline)  readyMissilePower(0 if no missile), readyShieldPower(0 if no shield)
		ServerMoon currentMoon = *it;
		result +="|Moon,";
		result += Utils::ftoa(currentMoon.getMoonId());
		result += ",";
		result += Utils::ftoa(currentMoon.getPlayerId())+",";
		result += Utils::ftoa(currentMoon.getPopulation())+",";
		result += Utils::ftoa(currentMoon.getMissileResearchLevel())+",";
		result += Utils::ftoa(currentMoon.getShieldResearchLevel())+",";
		result += Utils::ftoa(currentMoon.getReproduction())+",";		
		result += Utils::ftoa(currentMoon.getProduction())+",";
		result += Utils::ftoa(currentMoon.getResearch())+",";
		result += currentMoon.getCurrentProductionTaskName()+",";
		result += Utils::ftoa(currentMoon.getBuildProgress())+",";
		result += currentMoon.getCurrentResearchTaskName()+",";
		result += Utils::ftoa(currentMoon.getResearchProgress())+",";
		if(currentMoon.getReadyMissile() != NULL)
		{
			Missile missile = *currentMoon.getReadyMissile();
			result += Utils::ftoa(missile.getPower())+",";
		}
		else
		{
			result += "-1,";
		}
		if(currentMoon.getReadyShield() != NULL)
		{
			
			Shield shield = *currentMoon.getReadyShield();
			result += Utils::ftoa(shield.getPower())+"|";//end moon string
		}
		else
		{
			result += "-1";
		}

	}
	
	for(vector<ServerPlayer>::iterator it = (*allPlayers).begin(); it != (*allPlayers).end(); ++it)
	{
		ServerPlayer currentPlayer = *it;
		//player string
		result+="|Player,";
		result+=Utils::ftoa(currentPlayer.getPlayerId())+",";
		result+=currentPlayer.getPlayerName()+",";
		vector<int> moons = *currentPlayer.getMoons();
		for(vector<int>::iterator it2 = (moons).begin(); it2 != moons.end(); ++it2)
		{
			int moonId = *it2;
			result+= Utils::ftoa(moonId)+",";
		}
		
	}
	return result;
}